package calvin.study.rabbitmq.service.mq.impl;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import calvin.study.rabbitmq.service.mq.IMqService;

@Service
public class TTLExchangeService implements IMqService {
	@Autowired
	private RabbitTemplate rabbitTemplate;

	@Override
	public void pushMsg(String msg) throws Exception {
		rabbitTemplate.convertAndSend("ttl_direct_exchange", "ttl", msg);
	}

	public void TTLMsg(String msg) throws Exception {
		// 设置消息参数
		MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				message.getMessageProperties().setExpiration("5000");
				message.getMessageProperties().setContentEncoding("UTF-8");

				return message;
			}
		};

		rabbitTemplate.convertAndSend("ttl_direct_exchange", "ttl2", msg, messagePostProcessor);
	}

	public void ttl2DeadLetter(String msg) throws Exception {
		// 设置消息参数
		MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
				message.getMessageProperties().setExpiration("5000");
				message.getMessageProperties().setContentEncoding("UTF-8");

				return message;
			}
		};

		rabbitTemplate.convertAndSend("ttl_direct_exchange", "ttl2dead", msg, messagePostProcessor);
	}

}
